package o;

import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a;

public final class agj implements TsPayloadReader {
    private int ʻ;
    private int ʼ;
    private boolean ʽ;
    private int ˊ;
    private int ˋ = 0;
    private long ˋॱ;
    private final agc ˎ;
    private anq ˏ;
    private boolean ˏॱ;
    private final ane ॱ = new ane(new byte[10]);
    private boolean ॱॱ;
    private boolean ᐝ;

    public agj(agc o_agc) {
        this.ˎ = o_agc;
    }

    public void ˎ(anq o_anq, ady o_ady, a aVar) {
        this.ˏ = o_anq;
        this.ˎ.ˏ(o_ady, aVar);
    }

    public final void ˏ() {
        this.ˋ = 0;
        this.ˊ = 0;
        this.ᐝ = false;
        this.ˎ.ˏ();
    }

    public final void ॱ(anc o_anc, int i) throws ParserException {
        if ((i & 1) != 0) {
            switch (this.ˋ) {
                case 0:
                case 1:
                    break;
                case 2:
                    amz.ˏ("PesReader", "Unexpected start indicator reading extended header");
                    break;
                case 3:
                    if (this.ʼ != -1) {
                        amz.ˏ("PesReader", "Unexpected start indicator: expected " + this.ʼ + " more bytes");
                    }
                    this.ˎ.ˋ();
                    break;
                default:
                    throw new IllegalStateException();
            }
            ˏ(1);
        }
        while (o_anc.ˊ() > 0) {
            int i2;
            switch (this.ˋ) {
                case 0:
                    o_anc.ˎ(o_anc.ˊ());
                    break;
                case 1:
                    if (!ˏ(o_anc, this.ॱ.ˎ, 9)) {
                        break;
                    }
                    ˏ(ˊ() ? 2 : 0);
                    break;
                case 2:
                    if (ˏ(o_anc, this.ॱ.ˎ, Math.min(10, this.ʻ)) && ˏ(o_anc, null, this.ʻ)) {
                        ˎ();
                        if (this.ˏॱ) {
                            i2 = 4;
                        } else {
                            i2 = 0;
                        }
                        i |= i2;
                        this.ˎ.ˋ(this.ˋॱ, i);
                        ˏ(3);
                        break;
                    }
                case 3:
                    int i3;
                    i2 = o_anc.ˊ();
                    if (this.ʼ == -1) {
                        i3 = 0;
                    } else {
                        i3 = i2 - this.ʼ;
                    }
                    if (i3 > 0) {
                        i2 -= i3;
                        o_anc.ॱ(o_anc.ˋ() + i2);
                    }
                    this.ˎ.ˊ(o_anc);
                    if (this.ʼ == -1) {
                        break;
                    }
                    this.ʼ -= i2;
                    if (this.ʼ != 0) {
                        break;
                    }
                    this.ˎ.ˋ();
                    ˏ(1);
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
    }

    private void ˏ(int i) {
        this.ˋ = i;
        this.ˊ = 0;
    }

    private boolean ˏ(anc o_anc, byte[] bArr, int i) {
        int min = Math.min(o_anc.ˊ(), i - this.ˊ);
        if (min <= 0) {
            return true;
        }
        if (bArr == null) {
            o_anc.ˎ(min);
        } else {
            o_anc.ˎ(bArr, this.ˊ, min);
        }
        this.ˊ = min + this.ˊ;
        if (this.ˊ != i) {
            return false;
        }
        return true;
    }

    private boolean ˊ() {
        this.ॱ.ˋ(0);
        int ॱ = this.ॱ.ॱ(24);
        if (ॱ != 1) {
            amz.ˏ("PesReader", "Unexpected start code prefix: " + ॱ);
            this.ʼ = -1;
            return false;
        }
        this.ॱ.ˎ(8);
        int ॱ2 = this.ॱ.ॱ(16);
        this.ॱ.ˎ(5);
        this.ˏॱ = this.ॱ.ˊ();
        this.ॱ.ˎ(2);
        this.ॱॱ = this.ॱ.ˊ();
        this.ʽ = this.ॱ.ˊ();
        this.ॱ.ˎ(6);
        this.ʻ = this.ॱ.ॱ(8);
        if (ॱ2 == 0) {
            this.ʼ = -1;
        } else {
            this.ʼ = ((ॱ2 + 6) - 9) - this.ʻ;
        }
        return true;
    }

    private void ˎ() {
        this.ॱ.ˋ(0);
        this.ˋॱ = -9223372036854775807L;
        if (this.ॱॱ) {
            this.ॱ.ˎ(4);
            long ॱ = ((long) this.ॱ.ॱ(3)) << 30;
            this.ॱ.ˎ(1);
            ॱ |= (long) (this.ॱ.ॱ(15) << 15);
            this.ॱ.ˎ(1);
            ॱ |= (long) this.ॱ.ॱ(15);
            this.ॱ.ˎ(1);
            if (!this.ᐝ && this.ʽ) {
                this.ॱ.ˎ(4);
                long ॱ2 = ((long) this.ॱ.ॱ(3)) << 30;
                this.ॱ.ˎ(1);
                ॱ2 |= (long) (this.ॱ.ॱ(15) << 15);
                this.ॱ.ˎ(1);
                ॱ2 |= (long) this.ॱ.ॱ(15);
                this.ॱ.ˎ(1);
                this.ˏ.ˏ(ॱ2);
                this.ᐝ = true;
            }
            this.ˋॱ = this.ˏ.ˏ(ॱ);
        }
    }
}
